//
//  YZDWAPI.m
//  VET
//
//  Created by Lam BG on 2018/7/12.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZWorkbenchAPI.h"

#import "YZAutoFormHeader.h"

#import "NSString+DisplayTime.h"

@implementation YZWorkbenchAPI

#pragma mark - 工作台

+ (RACSignal*)index {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPIIndex;
//    api.showLoadingStatus = YES;
    
    return [api getRequest];
}

#pragma mark - 每日待办

/** 获取相关 item 下的任务列表 */
+ (instancetype)initWithItemType:(DWItemType)itemType {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPITask;
    
    [api addParams:@{@"taskStatus" : [@(itemType) stringValue]}];
    
    // 未完成 凌晨
    if (DWItemTypeUndo == itemType) [api addParams:@{@"startTime" : [NSString currentDayStartTime]}];
    // 已完成 1个月前 -> 现在
    if (DWItemTypeDone == itemType) {
        
        NSDate *date = [NSDate date];
        NSDate *startDate = [NSDate dateWithTimeInterval:-(24 * 60 * 60 * 30) sinceDate:date];
        NSUInteger startTime = [startDate timeIntervalSince1970];
        NSUInteger endTime   = [date timeIntervalSince1970];
        
        [api addParams:@{@"startTime" : [@(startTime) stringValue]}];
        [api addParams:@{@"endTime"   : [@(endTime) stringValue]}];
    }
    // 已忽略 1个月前 -> 凌晨
    if (DWItemTypeIgnored == itemType) {
        
        NSDate *date = [NSDate date];
        NSDate *startDate = [NSDate dateWithTimeInterval:-(24 * 60 * 60 * 30) sinceDate:date];
        NSUInteger startTime = [startDate timeIntervalSince1970];
        
        [api addParams:@{@"startTime" : [@(startTime) stringValue]}];
        [api addParams:@{@"endTime"   : [NSString currentDayStartTime]}];
    }
    
    return api;
}

/** 标记完成任务 0-未完成 1-已完成 2-已忽略 */
+ (RACSignal*)markDoneDailyWorkAPI:(NSString*)taskID {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITaskStataus, taskID];
    api.showLoadingStatus = YES;
    [api addParam:@"taskStatus" value:@"1"];
    
    return [api putRequest];
}

/** 标记未成任务 0-未完成 1-已完成 2-已忽略 */
+ (RACSignal*)markUndoDailyWorkAPI:(NSString*)taskID {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITaskStataus, taskID];
    api.showLoadingStatus = YES;
    [api addParam:@"taskStatus" value:@"0"];
    
    return [api putRequest];
}

/** 删除任务 */
+ (RACSignal*)deleteDailyWorkAPI:(NSString*)taskID {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITask, taskID];
    api.showLoadingStatus = YES;
    
    return [api deleteRequest];
}


#pragma mark - 新建任务

/** 新建任务 */
+ (RACSignal*)newDailyWorkAPI:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPITask;
    api.showLoadingStatus = YES;

    [api addParams:[api getDailyWorkParams:params]];
    
    return [api postRequest];
}

+ (RACSignal*)updateDailyWorkAPI:(NSString*)taskID params:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITask, taskID];
    api.showLoadingStatus = YES;

    [api addParams:[api getDailyWorkParams:params]];
    
    return [api putRequest];
}

// 提醒时间参数
- (NSMutableDictionary*)getDailyWorkParams:(NSMutableDictionary*)params {
    
    // 参数判断
    if (params[@"taskAlarm"]) {
        
        // 如果选择了截止时间
        AlarmTimeType alarmTime = (AlarmTimeType)[params[@"taskAlarm"] integerValue];
        // 配置提醒时间
        NSUInteger deadLine = [params[@"taskDeadline"] integerValue];
        switch (alarmTime) {
                
            case AlarmTimeType15M:
            {
                [params setObject:[@(deadLine - (15 * 60)) stringValue] forKey:@"alarmTime"];
            }
                break;
                
            case AlarmTimeType30M:
            {
                [params setObject:[@(deadLine - (30 * 60)) stringValue] forKey:@"alarmTime"];
            }
                break;
                
            case AlarmTimeType1H:
            {
                [params setObject:[@(deadLine - (1 * 60 * 60)) stringValue] forKey:@"alarmTime"];
            }
                break;
                
            case AlarmTimeType3H:
            {
                [params setObject:[@(deadLine - (3 * 60 * 60)) stringValue] forKey:@"alarmTime"];
            }
                break;
                
            case AlarmTimeType1D:
            {
                [params setObject:[@(deadLine - (24 * 60 * 60)) stringValue] forKey:@"alarmTime"];
            }
                break;
                
            default:
                break;
        }
        
    } else {
        
        // 默认选择 不提醒
        [params setObject:[@(AlarmTimeTypeNone) stringValue] forKey:@"taskAlarm"];
    }
    
    return params;
}

#pragma mark - 猪只

/** 获取猪只信息 */
+ (RACSignal*)getSwineInfoAPI:(NSString*)swineHerdId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPISwine, swineHerdId];;
    api.showLoadingStatus = YES;
    
    return [api getRequest];
    
}

/** 新建猪只 */
+ (RACSignal*)newSwineAPI:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPISwine;
    api.showLoadingStatus = YES;

    [api addParams:params];
    
    return [api postRequest];
}

/** 删除猪只 */
+ (RACSignal*)deleteSwineAPI:(NSString*)swineHerdId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPISwine, swineHerdId];
    api.showLoadingStatus = YES;
    
    return [api deleteRequest];
}

#pragma mark -

/** 猪只 新增治疗 */
+ (RACSignal*)treatSwineAPI:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPITreatmentSwine;
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api postRequest];
}

/** 猪只 删除治疗 */
+ (RACSignal*)deleteTreatSwineAPI:(NSString*)path {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITreatmentSwine, path];
    api.showLoadingStatus = YES;
    
    
    return [api deleteRequest];
}

#pragma mark - 猪群

/** 获取猪群信息 */
+ (RACSignal*)getSwineryInfoAPI:(NSString*)swineHerdId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPISwinery, swineHerdId];
    api.showLoadingStatus = YES;
    
    return [api getRequest];
}

/** 新建猪群 */
+ (RACSignal*)newSwineryAPI:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPISwinery;
    api.showLoadingStatus = YES;

    [api addParams:params];
    
    return [api postRequest];
}

/** 更新猪群 */
+ (RACSignal*)updateSwineryAPI:(NSString*)swineHerdId params:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPISwinery, swineHerdId];
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api putRequest];
}

/** 删除猪群 */
+ (RACSignal*)deleteSwineryAPI:(NSString*)swineHerdId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPISwinery, swineHerdId];
    api.showLoadingStatus = YES;
    
    return [api deleteRequest];
}

/** 结批猪群 */
+ (RACSignal*)FinishedSwineryAPI:(NSString*)swineHerdId params:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPIFinishedSwinery, swineHerdId];
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api putRequest];
}

/** 转入转出猪群 */
+ (RACSignal*)transferSwineryAPI:(NSString*)swineHerdId params:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPITansferSwinery, swineHerdId];
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api postRequest];
}

#pragma mark - 死淘

/** 死淘登记 */
+ (RACSignal*)deadSignAPIWithParams:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPIDieOut;
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api postRequest];
}

/** 死淘删除 */
+ (RACSignal*)deleteDeadSignAPIWithParams:(NSString*)dieOutId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPIDieOut, dieOutId];
    api.showLoadingStatus = YES;
    
    
    return [api deleteRequest];
}

#pragma mark - 免疫

/** 添加免疫 */
+ (RACSignal*)vaccinationAPIWithParams:(NSMutableDictionary*)params {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = kAPIVaccination;
    api.showLoadingStatus = YES;
    
    [api addParams:params];
    
    return [api postRequest];
}

/** 获取猪群免疫详情 */
+ (RACSignal*)vaccinationImmuneSwineryAPIWithParams:(NSString*)ids {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPIVaccination, ids];
    api.showLoadingStatus = YES;
    
    [api addParams:@{@"objectType" : @"herd"}];
    
    return [api getRequest];
}

/** 获取猪只免疫详情 */
+ (RACSignal*)vaccinationImmuneSwineAPIWithParams:(NSString*)ids {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPIVaccination, ids];
    api.showLoadingStatus = YES;
    
    [api addParams:@{@"objectType" : @"swine"}];
    
    return [api getRequest];
}

/** 删除免疫 */
+ (RACSignal*)deleteVaccinationAPIWithParams:(NSString*)vaccinationObjectId {
    
    YZWorkbenchAPI *api = [YZWorkbenchAPI api];
    api.action = [NSString stringWithFormat:@"%@/%@", kAPIVaccination, vaccinationObjectId];
    api.showLoadingStatus = YES;
    
    return [api deleteRequest];
}

@end












